package com.airbat.prize.utils;

import com.airbat.prize.common.enums.DataTypeEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 标签工具类
 */
public class TagUtil {

    /**
     * 将配置语法变成实体类
     *
     * @param tagId
     * @param conf
     * @return
     */
    public static List<TagConfig> getTagConfig(Long tagId, String conf) {
        //案件:{[(条件1||条件2)&&条件3],[...]}
        List<TagConfig> tagConfigs = new ArrayList<>();
        if (StringUtils.isEmpty(conf))
            return tagConfigs;
        try {
            String[] confTypes = conf.split("}");
            for (String confType : confTypes) {
                TagConfig tagConfig = new TagConfig();
                tagConfig.setTagId(tagId);
                List<TagConfigItem> items = new ArrayList<>();

                if (confType.contains("案件:")) {
                    tagConfig.dataType = DataTypeEnum.CASE;
                } else if (confType.contains("警情:")) {
                    tagConfig.dataType = DataTypeEnum.POLICEINFO;
                } else if (confType.contains("行业:")) {
                    tagConfig.dataType = DataTypeEnum.INDUSTRY;
                }

                confType = confType.substring(confType.indexOf("["), confType.length());
                String[] conditions = confType.split("],\\[");
                for (String condition : conditions) {
                    TagConfigItem tagConfigItem = new TagConfigItem();
                    List<String> orList = new ArrayList<>();
                    List<String> andList = new ArrayList<>();

                    condition = condition.replaceAll("\\[", "").replaceAll("\\]", "");
                    if (condition.contains(")")) {
                        String conditionTemp = condition.substring(condition.indexOf("(") + 1, condition.indexOf(")"));
                        String[] ors = conditionTemp.split("\\|\\|");
                        for (String or : ors) {
                            orList.add(or.replaceAll("\\(", "").replaceAll("\\)", ""));
                        }
                        tagConfigItem.setOrs(orList);

                        String conditionTemp2 = condition.substring(condition.indexOf(")&&") + 3, condition.length());
                        String[] ands = conditionTemp2.split("&&");
                        for (String and : ands) {
                            andList.add(and);
                        }
                        tagConfigItem.setAnds(andList);
                    } else if (condition.contains("||")) {
                        String[] ors = condition.split("\\|\\|");
                        for (String or : ors) {
                            orList.add(or);
                        }
                        tagConfigItem.setOrs(orList);
                    } else if (condition.contains("&&")) {
                        String[] ands = condition.split("&&");
                        for (String and : ands) {
                            andList.add(and);
                        }
                        tagConfigItem.setAnds(andList);
                    }
                    items.add(tagConfigItem);
                }
                tagConfig.setItems(items);
                tagConfigs.add(tagConfig);
            }
        } catch (Exception ex) {

        }
        return tagConfigs;
    }

    /**
     * 计算是否有符合的标签
     *
     * @param tagConfigs
     * @param content
     * @return
     */
    public static List<Long> existTags(List<TagConfig> tagConfigs, String content) {
        List<Long> tagIds = new ArrayList<>();
        if (StringUtils.isEmpty(content))
            return tagIds;

        for (TagConfig tagConfig : tagConfigs) {
            boolean isOK = false;
            for (TagConfigItem tagConfigItem : tagConfig.getItems()) {
                boolean isAnd = true;
                boolean isOr = true;
                if (tagConfigItem.getAnds() != null && tagConfigItem.getAnds().size() > 0) {
                    isAnd = false;
                    Integer andCount = 0;
                    for (String and : tagConfigItem.getAnds()) {
                        if (content.contains(and)) {
                            andCount++;
                        }
                    }
                    if (andCount >= tagConfigItem.getAnds().size())
                        isAnd = true;
                }
                if (tagConfigItem.getOrs() != null && tagConfigItem.getOrs().size() > 0) {
                    isOr = false;
                    for (String or : tagConfigItem.getOrs()) {
                        if (content.contains(or)) {
                            isOr = true;
                            break;
                        }
                    }
                }
                if (isAnd && isOr) {
                    isOK = true;
                    break;
                }
            }
            if (isOK) {
                tagIds.add(tagConfig.getTagId());
            }
        }
        return tagIds;
    }

    @Data
    public static class TagConfig {
        private Long tagId;
        private DataTypeEnum dataType;
        private List<TagConfigItem> items;
    }

    @Data
    public static class TagConfigItem {
        private List<String> ors;
        private List<String> ands;
    }

}
